package com.igeek.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.yygh.MD5;
import com.igeek.yygh.exception.YyghException;
import com.igeek.yygh.model.hosp.HospitalSet;
import com.igeek.yygh.result.Result;
import com.igeek.yygh.result.ResultCodeEnum;
import com.igeek.yygh.service.HospitalSetService;
import com.igeek.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Author zengxiangjun
 * @Date 2022/8/1  21:36 HospitalSetController
 */


@RestController
@RequestMapping("/admin/hosp/hospSet")
@Slf4j
@Api(tags = "医院设置接口")
//跨域标签处理
@CrossOrigin
public class HospitalSetController {
    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    // postman测试地址： http://localhost:8891/admin/hosp/hospSet/findAll
    //Swagger测试地址：http://localhost:8891/swagger-ui.html
    @ApiOperation(value = "查看医院设置列表")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> list = hospitalSetService.list();


        //测试全局异常处理器
        try {
            int i = 10 / 0;

        } catch (Exception e) {
            //抛出自定义异常
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }


        return Result.ok(list);
    }

    //逻辑删除医院设置
    @ApiOperation(value = "根据编号删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteById(
            @ApiParam(name = "id", value = "医院设置编号", required = true) @PathVariable("id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //参数列表中是请求参数query，查询搭配@GetMapping
    //参数列表中是请求参数body，即为json数据格式，查询搭配@PostMapping
    @ApiOperation(value = "分页条件查看医院设置列表")
    @PostMapping("findAllByPage/{pageNow}/{limit}")
    public Result findAllByPage(
            @ApiParam(name = "pageNow", value = "当前页码", required = true) @PathVariable("pageNow") Integer pageNow,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Integer limit,
            @RequestBody HospitalSetQueryVo hospitalSetQueryVo
    ) {
        //封装Page对象
        Page<HospitalSet> page = new Page<>(pageNow, limit);

        //封装查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        //获取医院名称
        String hosname = hospitalSetQueryVo.getHosname();
        if (StringUtils.hasLength(hosname)) {
            queryWrapper.like("hosname", hosname);
        }
        //获取医院编号
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (StringUtils.hasLength(hoscode)) {
            queryWrapper.like("hoscode", hoscode);
        }

        //分页查询
        Page<HospitalSet> pageModel = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageModel);
    }


    @ApiOperation("新增医院设置信息")
    @PostMapping("/add")
    public Result add(@ApiParam("医院设置信息") @RequestBody HospitalSet hospitalSet) {
        //签名秘钥
        String signKey = MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000));
        hospitalSet.setSignKey(signKey);
        //添加数据
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation("通过医院设置编号查询信息")
    @GetMapping("findOne/{id}")
    public Result<HospitalSet> findOne(@ApiParam("医院设置编y号") @PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation("修改医院设置信息")
    @PutMapping("/update")
    public Result update(@ApiParam("医院设置信息") @RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation("批量删除医院设置信息")
    @DeleteMapping("/deleteAll")
    public Result deleteAll(@ApiParam("批量医院设置编号") @RequestBody List<Long> ids) {
        boolean flag = hospitalSetService.removeBatchByIds(ids);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation("医院锁定与解锁")
    @PutMapping("lock/{id}/{status}")
    public Result lockHospitalSet(
            @ApiParam("医院设置编号") @PathVariable("id") Long id,
            @ApiParam("医院的锁定状态 1解锁 0锁定") @PathVariable("status") Integer status
    ) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        /*if(hospitalSet==null){
            throw new YyghException(ResultCodeEnum.HOSPITAL_LOCK);
        }*/
        if (hospitalSet != null) {
            hospitalSet.setStatus(status);
            boolean flag = hospitalSetService.updateById(hospitalSet);
            if (flag) {
                return Result.ok();
            }
        }
        return Result.fail();
    }

    //发送签名密钥
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKeyHospSet/{id}")
    public Result sendKeyHospSet(
            @ApiParam(name = "id", value = "医院设置编号") @PathVariable Long id
    ) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //发送短信
        return Result.ok();
    }
}
